package zhongbo.java.air.auth.web.controller;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhongbo.java.air.auth.service.IAuthService;
import zhongbo.java.air.auth.vo.AuthLoginVo;
import zhongbo.java.air.auth.vo.AuthResponseVo;
import zhongbo.java.air.common.util.HttpServletUtil;
import zhongbo.java.air.common.vo.ResultVo;

/**
 * 说明：
 *
 * @author 钟波dellNotebook
 * @since 2025年03月29日 星期六 14:47:31
 */
@RestController
@RequestMapping("auth")
@Api(tags = "权限接口")
public class AuthController {

    @Autowired
    private IAuthService authService;

    /**
     * 用户注册
     *
     * @param authLogin 登录信息
     * @return 注册成功后的用户信息或令牌
     */
    @ApiOperation("用户注册")
    @PostMapping("register")
    ResultVo<AuthResponseVo> register(@RequestBody AuthLoginVo authLogin) {
        String username = authLogin.getUsername();
        String password = authLogin.getPassword();
        AuthResponseVo responseVo = authService.register(username, password);
        return ResultVo.ok(responseVo);
    }

    /**
     * 用户登录
     *
     * @param authLogin 登录信息
     * @return 认证成功后的用户信息或令牌
     */
    @ApiOperation("用户登录")
    @PostMapping("login")
    ResultVo<AuthResponseVo> login(@RequestBody AuthLoginVo authLogin) {
        String username = authLogin.getUsername();
        String password = authLogin.getPassword();
        AuthResponseVo responseVo = authService.login(username, password);
        return ResultVo.ok(responseVo);
    }

    /**
     * 用户登出
     */
    @ApiOperation("用户登出")
    @GetMapping("logout")
    ResultVo<?> logout() {
        String token = HttpServletUtil.getHeaderToken();
        if (StrUtil.isNotBlank(token)) {
            authService.logout(token);
        }
        return ResultVo.success();
    }

    /**
     * 验证令牌
     *
     * @param token 令牌
     * @return 验证结果
     */
    @ApiOperation("验证令牌")
    @GetMapping("validateToken")
    ResultVo<Boolean> validateToken(String token) {
        boolean validateToken = authService.validateToken(token);
        return ResultVo.ok(validateToken);
    }

    /**
     * 获取登录信息
     *
     * @return 用户信息或令牌
     */
    @ApiOperation("获取登录信息")
    @GetMapping("getTokenInfo")
    ResultVo<AuthResponseVo> getTokenInfo() {
        String token = HttpServletUtil.getHeaderToken();
        AuthResponseVo tokenInfo = authService.getTokenInfo(token);
        return ResultVo.ok(tokenInfo);
    }

    /**
     * 刷新令牌
     *
     * @return 新的令牌
     */
    @ApiOperation("刷新令牌")
    @GetMapping("refreshToken")
    ResultVo<AuthResponseVo> refreshToken() {
        String token = HttpServletUtil.getHeaderToken();
        AuthResponseVo responseVo = authService.refreshToken(token);
        return ResultVo.ok(responseVo);
    }

}
